package com.societyGames.flashForms
{
import flash.events.Event;
import flash.geom.Rectangle;

/*Centers a textfield horizontally and vertically*/
public class TextCenterContainer extends SingleItemContainer
{
  private var _desiredWidth:Number;
  private var _desiredHeight:Number;
  private var _isInRecalculate:Boolean = false;

  public override function set width(value:Number):void
  {
    this._desiredWidth = value;
    recalculate();
  }

  public override function set height(value:Number):void
  {
    this._desiredHeight = value;
    recalculate();
  }

  protected override function childSizeChangedHandler(event:Event):void
  {
    if (!_isInRecalculate)
    {
      recalculate();
    }
  }

  private function recalculate():void
  {
    _isInRecalculate = true;
    if (this._item != null)
    {
      this._item.width = _desiredWidth;
      this._item.height = _desiredHeight;

      //Center the 0,0 first.
      this._item.x = this._desiredWidth * 0.5;
      this._item.y = this._desiredHeight * 0.5;
      //Now shift it over.
      var bounds:Rectangle = this._item.getBounds(this);
      this._item.x += this._item.x - (bounds.left + 0.5 * bounds.width);
      this._item.y += this._item.y - (bounds.top + 0.5 * bounds.height);
    }
    _isInRecalculate = false;
  }
}
}